package com.xty.xtyle.base;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xty.xtyle.R;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.detail.ClothDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 统计列表中点击的具体衣服列表
 */
public class ListActivity extends AppCompatActivity {

    /**
     * 打开本页面
     * @param context
     * @param type   类型,比如颜色,面料等
     * @param value 类型中的具体值,比如红色,面料的步等等
     */
    public static void startActivity(Activity context, String type, String value) {
        Intent intent = new Intent(context, ListActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("value", value);
        context.startActivity(intent);
    }

    private RecyclerView recyclerView;
    private String[] types = {"类型","颜色","面料","季节","品牌","状态"};
    List<Cloth> contentCloth = new ArrayList<>();
    List<Cloth> cloths;
    String type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        recyclerView = findViewById(R.id.rv);


        findViewById(R.id.left_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.right_icon).setVisibility(View.GONE);

        ClothDao dao = new ClothDao();
        cloths = dao.findAll();

        type = getIntent().getExtras().getString("type");//类型，可以，不可用
        TextView textView = findViewById(R.id.center_icon);
        textView.setText("统计："+type);
        String value = getIntent().getExtras().getString("value");//类型，可以，不可用
        if (null == cloths || cloths.isEmpty()) return;
        switch (type) {
            case "类型":
                checkType(value);
                break;
            case "颜色":
                checkColor(value);
                break;
            case "面料":
                checkMaterial(value);
                break;
            case "季节":
                checkSession(value);
                break;
            case "品牌":
                checkBrand(value);
                break;
            case "状态":
                checkStatus(value);
                break;
        }

        recyclerView.setLayoutManager(new GridLayoutManager(this,1));
        recyclerView.setAdapter(new StringAdapter());
    }

    private void checkStatus(String value) {
        for (Cloth cloth:cloths) {
            String color = cloth.getStatus();
            if (!value.equals(color)) continue;
            contentCloth.add(cloth);
        }
    }

    private void checkBrand(String value) {
        for (Cloth cloth:cloths) {
            String color = cloth.getBrand();
            if (!value.equals(color)) continue;
            contentCloth.add(cloth);
        }
    }

    private void checkSession(String value) {
        for (Cloth cloth:cloths) {
            String color = cloth.getSession();
            if (!value.equals(color)) continue;
            contentCloth.add(cloth);
        }
    }

    private void checkMaterial(String value) {
        for (Cloth cloth:cloths) {
            String color = cloth.getMaterial();
            if (!value.equals(color)) continue;
            contentCloth.add(cloth);
        }
    }

    private void checkColor(String value) {
        for (Cloth cloth:cloths) {
            String color = cloth.getColor();
            if (!value.equals(color)) continue;
            contentCloth.add(cloth);
        }
    }

    private void checkType(String value) {
        for (Cloth cloth:cloths) {
            String type = cloth.getType();
            if (!value.equals(type)) continue;
            contentCloth.add(cloth);
        }
    }


    private class StringAdapter extends RecyclerView.Adapter<StringAdapter.VH> {

        @NonNull
        @Override
        public StringAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            return new StringAdapter.VH(LayoutInflater.from(parent.getContext()).inflate( R.layout.item_analysis, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull StringAdapter.VH holder, int position) {

                final Cloth cl = contentCloth.get(position);
                if (null != cl) {
                    Glide.with(holder.analysisImage).load(cl.getImage()).into(holder.analysisImage);
                }
                holder.textContent.setText(getRes(cl, type));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ClothDetailActivity.startActivity(view.getContext(), cl.getId());
                    }
                });
        }

        @Override
        public int getItemCount() {
            return contentCloth.size();
        }

        class VH extends RecyclerView.ViewHolder {
            TextView textView;
            TextView textContent;
            ImageView analysisImage;
            public VH(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_title);
                textContent = itemView.findViewById(R.id.tv_content);
                analysisImage = itemView.findViewById(R.id.analysisIcon);
            }
        }
    }

    String getRes(Cloth cloth, String type) {
        String value = "";
        switch (type) {
            case "类型":
                value= cloth.getType();
                break;
            case "颜色":
                value= cloth.getColor();
                break;
            case "面料":
                value= cloth.getMaterial();
                break;
            case "季节":
                value= cloth.getSession();
                break;
            case "品牌":
                value= cloth.getBrand();
                break;
            case "状态":
                value= cloth.getStatus();
                break;
        }
        return value;
    }

}
